package cn.lhx.blog.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.ArticleFormBean;
import cn.lhx.blog.formbean.CommentFormBean;
import cn.lhx.blog.pojo.Article;
import cn.lhx.blog.pojo.Comment;
import cn.lhx.blog.pojo.User;
import cn.lhx.blog.service.ArticleService;
import cn.lhx.blog.service.CommentService;
import cn.lhx.blog.util.BlogInfo;
import cn.lhx.blog.util.PagenationTookit;



public class CommentAction extends DispatchAction{
	@Resource
   private CommentService commentService;
	@Resource
	private ArticleService articleService;
	public ActionForward addComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
			
		CommentFormBean comf=(CommentFormBean)form;
				//提取评论
				Comment comment =new Comment();
				log.info("添加评论");
				comment.setArticle(articleService.getArtilceById(comf.getArticle_id()));
				comment.setAuthor(comf.getAuthor());
				comment.setContent(comf.getContent());
				comment.setEmail(comf.getEmail());
				comment.setPostTime(new Date());
				comment.setUrl(comf.getUrl());
				//保存评论
				commentService.saveComment(comment);
				//保存发表评论的当前用户的信息到Session
				User curGuest=new User();
				curGuest.setName(comf.getAuthor());
				curGuest.setEmail(comf.getEmail());
				curGuest.setUrl(comf.getUrl());
				request.getSession().setAttribute("curGuest", curGuest);
				//跳转会日志查看：http://localhost:8080/SSHMyBlog/blog/1
				
				StringBuffer path=new StringBuffer("/blog/");
				path.append(comf.getArticle_id());
				path.append("#comment-"+comment.getId());
				ActionForward okForward =new ActionForward(path.toString());
				okForward.setRedirect(true);
				
			return okForward;
	}
	public ActionForward loadComments(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
		CommentFormBean comf=(CommentFormBean)form;
		// 获取当前页码
		int page = comf.getPage();
		page = page < 1 ? 1 : page;
		// 获得总共有多少篇日志,变
		long commentCount = commentService.statsCommentCount();
		// 计算出页数(pages)=articleCount/6
		int pages = commentCount % BlogInfo.BLOGS_ONE_PAGE != 0 ? (int) (commentCount
				/ BlogInfo.BLOGS_ONE_PAGE + 1)
				: (int) (commentCount / BlogInfo.BLOGS_ONE_PAGE);
		page = page > pages ? pages : page;
		log.info("page= " + page);
		request.setAttribute("pages", pages);
		request.setAttribute("curPage", page);
		// 设置页码导航条
		int nav[] = PagenationTookit.getPageNavigator(page,
				BlogInfo.COUNT_OF_PAGENUMBER_OF_MAINPAGE, pages);
		request.setAttribute("nav", nav);
		int base = page - 1;// 基点=第n页 -1
		// 读取几篇日志，变
		List<Comment> comments =commentService.getComments(base
				* BlogInfo.BLOGS_ONE_PAGE, BlogInfo.BLOGS_ONE_PAGE);
		request.setAttribute("comments", comments);
	
		log.info("执行初始操作！即将进入博客主页面");
		return mapping.findForward("comment_manager");
	}
	
	/**
	 * 删除日志
	 * 
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		CommentFormBean aticf = (CommentFormBean) form;
		int _id = aticf.getId();
		// 删除日志

		commentService.delComment(_id);
		log.info("评论被删除了 Id= " + _id);
		return mapping.findForward("deleteOK");
	}
	/**
	 * 设置批准状态
	 * @param mapping
	 * @param form
	 * @param request
	 * @param arg3
	 * @return
	 */
	public ActionForward setApproveState(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3) {
		CommentFormBean comtf = (CommentFormBean) form;
			int id=comtf.getId();
			String status=comtf.getStatus();
//			boolean isApproved=comtf.isApproved();
//			log.info("isApproved= "+isApproved);
			commentService.setStatus(id, status);
			return mapping.findForward("setOK");
	}
	
}
